package com.all.ui;

import com.all.utils.DialogUtils;
import com.all.utils.StringUtils;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;

@SuppressLint("CommitPrefEdits")
public class Preferences extends Activity{

	SharedPreferences mPreferences;
	Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preference);
 
        mContext = this;
        final CheckBox check =(CheckBox)findViewById(R.id.locat_check);       
        final EditText time = (EditText)findViewById(R.id.time_between);
        mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        check.setChecked(mPreferences.getBoolean("isLocation", false));
        time.setText(mPreferences.getString("time_bwtween", "3"));
        check.setOnCheckedChangeListener(new OnCheckedChangeListener (){
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				SharedPreferences.Editor edit =mPreferences.edit();
				if(check.isChecked())
				{
					if(StringUtils.isEmpty(time.getText().toString()))
					{
						DialogUtils.showInputDialog(mContext, "��������ʱ��");
						check.setChecked(false);
						return ;
					}else
					{
						edit.putString("time_bwtween", time.getText().toString());
						edit.putBoolean("isLocation", true);
						Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
						DialogUtils.showIntentDialog(mContext,"���GPS����",intent);
					}	
				}else
				{
					edit.putString("time_bwtween","");
					edit.putBoolean("isLocation", false);
				}
				edit.commit();
			} 	
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}
